'use strict';
var observeInstance = {
  params: [],
  eventList: {},
  addContextParams: function (property, value, op) {
    var obj = {};
    obj["k"] = property;
    obj["op"] = op || "=";
    obj["v"] = value;
    this.params.push(obj);
  },
  listen: function (key, fn) {
    if (!this.eventList[key]) {
      this.eventList[key] = [];
    }
    this.eventList[key].push(fn);
  },
  trigger: function () {
    var that = this;
    var key = Array.prototype.shift.call(arguments);
    var fnList = this.eventList[key];
    if (fnList && fnList.length > 0) {
      for (var index = 0; index < fnList.length; index++) {
        var fn = fnList[index];
        fn.apply(that, arguments);
      }
    } else {
      return false
    }
  },
  clear: function () {
    this.params = [];
  },
  remove: function (key, fn) {
    var fnList = this.eventList[key];
    if (fnList && fnList.length > 0) {
      if (!fn) {
        delete this.eventList[key];
      } else {
        for (var index = 0; index < fnList.length; index++) {
          var f = fnList[index];
          if (f === fn) {
            fnList.splice(index, 1);
          }
        }
      }
    } else {
      return false
    }
  }
}

module.exports = observeInstance;